package cn.org.july.weixin.service;

import cn.org.july.weixin.common.constant.MessageType;
import cn.org.july.weixin.common.interfaces.IMessage;
import cn.org.july.weixin.service.config.WxConfig;
import cn.org.july.weixin.service.message.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Map;

import static cn.org.july.weixin.common.constant.MessageType.*;

/**
 * 微信处理Service入口类
 *
 * @auther: wanghongjie
 * @blame: wanghongjie
 * @date: 2020-08-30 18:09
 * @Description:
 */
@Slf4j
@Service
public class WxService {

    private IMessage message;
    @Resource
    private WxConfig config;
    @Resource
    private EventService eventService;

    /**
     * 在接入之前执行方法
     */
    private void initBefore(String type) {
        MessageType messageType = getType(type);
        switch (messageType) {
            case LINK:
                message = new LinkMessageService();
                break;
            case EVENT:
                message = eventService;
                break;
            case IMAGE:
                message = new ImageMessageService();
                break;
            case VIDEO:
                message = new VideoMessageService();
                break;
            case SHORTVIDEO:
                message = new ShortVideoMessageService();
                break;
            case LOCATION:
                message = new LocationMessageService();
                break;
            case TEXT:
            default:
                message = new TextMessageService(config);
                break;
        }
    }


    /**
     * 返回消息后执行方法
     */
    protected void initAfter() {
    }

    /**
     * @param type 消息请求类型
     * @param map  请求参数
     * @return
     */
    public String initialize(String type, Map<String, Object> map) {
        initBefore(type);
        String msg = message.handler(map, null);
        initAfter();
        log.info("应答消息[{}]", msg);
        return msg;
    }
}
